#include <glib.h>

typedef struct employee_info_tag {
    gint id;
    gchar *name;
}employee_info_t;

static void _list_struct_print_func(gpointer data, gpointer user_data)
{
    employee_info_t *einfo = NULL;

    einfo = (employee_info_t *)data;
    if(NULL == einfo) {
        g_print("input param error! \n");
        return;
    }

    g_print("user_data:%s, einfo->id:%d, einfo->name:%s \n", (gchar *)user_data, einfo->id, einfo->name);
}

static void _list_struct_free_func(gpointer data)
{
    employee_info_t *einfo = NULL;

    einfo = (employee_info_t *)data;

    if( (NULL != einfo) && (NULL != einfo->name) ) {
        g_free(einfo->name);
    }

    if(NULL != einfo) {
        g_free(einfo);
    }
}

gint main(gint argc, gchar **argv)
{
    GList *list = NULL;
    GList *l = NULL;

    employee_info_t *einfo[4] = {NULL, NULL, NULL, NULL};

    einfo[0] = g_new0(employee_info_t, 1);
    einfo[1] = g_new0(employee_info_t, 1);
    einfo[2] = g_new0(employee_info_t, 1);
    einfo[3] = g_new0(employee_info_t, 1);

    einfo[0]->id = 1000;
    einfo[0]->name = g_strdup("Jack");
    einfo[1]->id = 1001;
    einfo[1]->name = g_strdup("Pony");
    einfo[2]->id = 1002;
    einfo[2]->name = g_strdup("Robin");
    einfo[3]->id = 1003;
    einfo[3]->name = g_strdup("NoName");

    list = g_list_append(list, (gpointer)einfo[0]); //Jack
    list = g_list_prepend(list, (gpointer)einfo[1]); //Pony->Jack
    list = g_list_insert(list, (gpointer)einfo[2], 1); //Pony->Robin->Jack

    l = g_list_nth(list, 0);
    list = g_list_insert_before(list, l, (gpointer)einfo[3]); //NoName->Pony->Robin->Jack

    g_list_foreach(list, _list_struct_print_func, "GList");

    g_list_free_full(list, _list_struct_free_func);

    return 0;
}